unit uConfiguration;

interface

uses Forms, IniFiles, SysUtils, Dialogs, Registry, Windows;

type
  TConfigurations = class(TObject)
    iniFile: TiniFile; //Option file
  private
    //Get ini file for options
    function getIniFile(): TiniFile;

    //Merge tool location Getter and Setter
    function GetLocation(): string;
    procedure SetLocation(location: string);

    //Merge parameters Getter and Setter
    function GetParameters(): string;
    procedure SetParameters(parameters: string);

    //Confirm file cleaning Getter and Setter
    function GetConfirmFileClean(): Boolean;
    procedure SetConfirmFileClean(confirmFileClean: Boolean);

    //Confirm file cleaning Getter and Setter
    function GetDeleteToRecycleBin(): Boolean;
    procedure SetDeleteToRecycleBin(deleteFileToRecycleBin: Boolean);
  public
    constructor Create();
    //Merge tool location
    property Location: string read GetLocation write SetLocation;
    //Merge parameters
    property Parameters: string read GetParameters write SetParameters;
    //Confirm file cleaning
    property ConfirmFileClean: Boolean read GetConfirmFileClean write 
    SetConfirmFileClean;
    //Delete file to recycle bin when clean files
    property DeleteFileToRecycleBin: Boolean read GetDeleteToRecycleBin
    write SetDeleteToRecycleBin;

    //Set TSVNMerge as default merge tool for TortoiseSVN.
    procedure SetDefaultMergeTool();
  end;

const
  //Key name for options
  SECTION_NAME: string = 'MergeOptions';
  LOCATION_KEY: string = 'Location';
  PARAMETERS_KEY: string = 'Parameters';
  COMFIRM_KEY: string = 'ConfirmFileClean';
  DELTORECYCLEBIN_KEY:string = 'DeleteFileToRecycleBin';

  //Default merge parameters
  DEFAULT_PARAMETERS = '%mine %theirs';

implementation

constructor TConfigurations.Create();
begin
  self.iniFile := getIniFile();
end;

function TConfigurations.getIniFile(): TiniFile;
var
  appPath: string; //Application file path
  appName: string; //Application file name
  iniFileName: string; //Ini file name
  newTextFile: Text;
begin
  appPath := PChar(ExtractFilePath(Application.Exename));
  appName := PChar(ExtractFileName(Application.Exename));
  iniFileName := appPath + ChangeFileExt(ExtractFileName(appName), '.ini');

  //If ini file not exist, create it.
  if not FileExists(iniFileName) then
  begin
    AssignFile(newTextFile, iniFileName);
    ReWrite(newTextFile);
    CloseFile(newTextFile);
  end;

  result := Tinifile.Create(iniFileName);
end;

function TConfigurations.GetLocation(): string;
begin
  Result := iniFile.ReadString(SECTION_NAME, LOCATION_KEY, '');
end;

procedure TConfigurations.SetLocation(location: string);
begin
  iniFile.WriteString(SECTION_NAME, LOCATION_KEY, location);
end;

function TConfigurations.GetParameters(): string;
begin
  Result := iniFile.ReadString(SECTION_NAME, PARAMETERS_KEY, DEFAULT_PARAMETERS);
end;

procedure TConfigurations.SetParameters(parameters: string);
begin
  iniFile.WriteString(SECTION_NAME, PARAMETERS_KEY, parameters);
end;

function TConfigurations.GetConfirmFileClean(): Boolean;
begin
  Result := iniFile.ReadBool(SECTION_NAME, COMFIRM_KEY, True);
end;

procedure TConfigurations.SetConfirmFileClean(confirmFileClean: Boolean);
begin
  iniFile.WriteBool(SECTION_NAME, COMFIRM_KEY, confirmFileClean);
end;

function TConfigurations.GetDeleteToRecycleBin(): Boolean;
begin
  Result := iniFile.ReadBool(SECTION_NAME, DELTORECYCLEBIN_KEY, True);
end;

procedure TConfigurations.SetDeleteToRecycleBin(deleteFileToRecycleBin: Boolean);
begin
  iniFile.WriteBool(SECTION_NAME, DELTORECYCLEBIN_KEY, deleteFileToRecycleBin);
end;

procedure TConfigurations.SetDefaultMergeTool();
var
  diffCommandLine: string;
  sysRegistry: TRegistry;
  tips: string;
begin
  sysRegistry := TRegistry.Create();
  diffCommandLine := Application.ExeName + ' %mine %theirs %base %merged';

  with sysRegistry do
  begin
    RootKey := HKEY_CURRENT_USER;
    try
      try
        Openkey('Software\TortoiseSVN', false);
        WriteString('Merge', diffCommandLine);
      except on E: Exception do
        begin
          tips := 'Failed set registry.' + #13#10 + 'Error Message:' +
            E.Message;
          MessageDlg(PAnsiChar(tips), mtInformation, [mbOK], 0);
          Exit;
        end;
      end;
    finally
      CloseKey;
    end;
  end;
end;

end.
